<?php
namespace app\member\home;

use think\Db;
use think\Controller;

class Ajaxparam extends Controller
{
    private $uid;
    
    //构造函数-用户验证
    public function __construct(){
        $uid= (session("u_id"))?session("u_id"):0;
        if($uid<1){
            return myjson('0','登录已失效,请先登录');
        }
        $this->uid=$uid;
    }
    
    //用户签到
    public function sign(){
        $uid=$this->uid;
        
        $starttime=date('Y-m-d',time());
        $endtime=date('Y-m-d 23:59:59',time());
        $changeqd=Db::name('member_moneylog')->where(['type'=>'188','uid'=>$uid])
        ->whereTime('add_time','between',[strtotime($starttime),strtotime($endtime)])
        ->count();
        if($changeqd && $changeqd>0){
            return myjson('0','今天已经签到，请明天再来！');
        }
        
        Db::startTrans();
        try {
            $datag = get_global_setting();      //得到系统设置
            $jifen=$datag['qd_money'];          //得到签到奖励管理费金额
            
            //A:添加签到记录
            $data=array();
            $data['uid'] = $uid;
            $data['ip'] = get_client_ip();
            $data['add_time'] = time();
            $data['is_success'] = 1;
            $data['money'] = $jifen;
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $reg1=Db::name("member_qd")->insert($data);
            
            //B:更改用户金额
            $reg2=up_membMoney($uid,2,$jifen);
            
            //C:添加用户金额日志
            $info='每日签到，赠送管理费'.$jifen.'元';
            $nwqmoney=getuseinfo($uid);           //得到用户金额信息
            $reg3=add_Moneylog($uid,'188',$jifen,$nwqmoney['account_money'],$nwqmoney['back_money'],$nwqmoney['money_freeze'],$info);
            
            if($reg1 && $reg2 && $reg3){
                Db::commit();
                return myjson('1','签到成功');
            }else{
                Db::rollback();
                return myjson('0','签到失败');
            }
        } catch (\Exception $e){
            Db::rollback();
            return myjson('0','签到失败');
        }
    }
    
    
    //充值申请
    public function recharge_submit(){
        $uid=$this->uid;
        
        $param=getparam();
        $type=(isset($param['type']))?trim($param['type']):'';
        $money=(isset($param['money']))?trim($param['money']):'';
        $tran=(isset($param['username']))?trim($param['username']):'';
        $off_way=(isset($param['off_way']))?trim($param['off_way']):'';
        
        if(!in_array($type,['payoff','zfb_smzf','wx_smzf','zfb_smzf_surplus','zfb_smzf_calyx'])){
            return myjson('0','充值方式不正确!');
        };
        
        if(!is_money($money) || $money<=0){
            return myjson('0','充值金额无效!');
        };
        
        if($money<100){
            return myjson('0','充值金额不能少于100元!');
        }
        
        if($type=='payoff'){
            if(!preg_match('/^[0-9a-zA-Z_\x{4e00}-\x{9fa5}]+$/u', $tran)){
                return myjson('0','充值用户无效!');
            };
            
            if(!preg_match('/^[0-9a-zA-Z\x{4e00}-\x{9fa5}()]+$/u', $off_way)){
                return myjson('0','充值方式格式无效!');
            };
        }
        
        //添加锁
        $key='recharge_'.$uid;
        cache($key,null);
        $sign=time().random('6','123456789');
        cache($key,$sign);
        
        $membaccount = new \app\member\model\Account();
        $result=$membaccount->recharge_do($uid,$type,$money,$tran,$off_way,$sign);
        
        //移除锁
        cache($key,null);
        
        if($result['code']=='0'){
            return myjson('0',$result['msg']);
        }

        return myjson('1',$result['msg']);

    }
    
    //第三方扫码支付申请
    public function dsf_smzf(){
        $params=getparam();
        if(!isset($params['money']) || !is_money(trim($params['money']))){
            return myjson('0','金额输入无效');
        }
        $money=trim($params['money']);
        
        $order_no='CZ'.date('Ymdhis').random('6','123456789');
        $sendObj=new \app\member\model\Pay_Send;
        $sendresult=$sendObj->aliRedPay($order_no,$money*100,'/member/paysuccess','/member/paysuccess/paysuccess','pc');
        
        if($sendresult['code']=='0'){
            return myjson('0',$sendresult['msg']);
        }
        
        $zfbpay=array(
            'uid'=>$this->uid,
            'orderno'=>$order_no,
            'money'=>$money,
            'status'=>'0',
            'addtime'=>time(),
            'systemId'=>$sendresult['data']['tradeId']
        );
        Db::name("payzfb")->insert($zfbpay);        //添加第三方智多宝充值表
        
        return myjson('1',$sendresult['data']);
    }
    
    //提现申请
    public function withdraw_submit(){
        $uid=$this->uid;
        $param=getparam();
        if(!isset($param['bank_id']) || empty($param['bank_id']) || !is_numeric(trim($param['bank_id']))){
            return myjson('0','银行卡ID不能为空!');
        }
        $bank_id=trim($param['bank_id']);
        
        if(!isset($param['money']) || !is_money(trim($param['money']))){
            return myjson('0','金额输入不正确!');
        }
        
        if(!isset($param['paypwd']) || empty(trim($param['paypwd']))){
            return myjson('0','支付密码不能为空!');
        }
        if(!preg_match('/^[a-zA-Z0-9_]{6,18}$/', trim($param['paypwd']))){
            return myjson('0','请输入6-18位的有效支付密码!');
        };
        
        if(!isset($param['code']) || empty(trim($param['code'])) || !is_numeric(trim($param['code']))){
            return myjson('0','图片验证码不能为空!');
        }
        // 验证码
        if(!captcha_check(trim($param['code']))){
            return myjson('0','验证码错误或失效！');
        };
        
        $money=trim($param['money']);
        $useinfo=getuseinfo($uid);
        if(md5(trim($param['paypwd']))!=$useinfo['pin_pass']){
            return myjson('0','支付密码不正确!');
        }
        
        //添加锁
        $key='widthdraw_'.$uid;
        cache($key,null);
        $sign=time().random('6','123456789');
        cache($key,$sign);
        
        $membaccount = new \app\member\model\Account();
        $result=$membaccount->widthdraw_do($uid,$bank_id,$money,$sign);
        
        //移除锁
        cache($key,null);
        
        if($result['code']=='0'){
            return myjson('0',$result['msg']);
        }
        return myjson('1','提现申请成功，请等待审核!');
        
    }
    
    
    //删除消息
    public function delmsg(){
        $uid=$this->uid;
        $param=getparam();
        if(!isset($param['id']) || !is_numeric($param['id'])){
            return myjson('0','信息ID不能为空');
        }
        $checkid=db('inner_msg')->where(['id'=>trim($param['id']),'uid'=>$uid])->count();
        if(!$checkid || $checkid<1){
            return myjson('0','信息不存在');
        }
        $res=db('inner_msg')->where(['id'=>trim($param['id'])])->delete();
        if(!$res){
            return myjson('0','删除失败');
        }
        return myjson('1','删除成功');
    }
    
    
    //修改登录密码
    public function changePwd(){
        $params=getparam();
        if(!isset($params['oldpwd']) || empty($params['oldpwd'])){
            return myjson('0','原始登录密码不能为空!');
        }
        if(!preg_match('/^[a-zA-Z0-9_]{6,18}$/', trim($params['oldpwd']))){
            return myjson('0','原始密码格式不正确!');
        };
        $oldpwd=trim($params['oldpwd']);
        
        if(!isset($params['pwd']) || empty($params['pwd'])){
            return myjson('0','新登录密码不能为空!');
        }
        if(!preg_match('/^[a-zA-Z0-9_]{6,18}$/', trim($params['pwd']))){
            return myjson('0','新密码格式不正确!');
        };
        $pwd=trim($params['pwd']);
        
        $uid=$this->uid;
        $useinfo=getuseinfo($uid);
        if(md5($oldpwd)!=$useinfo['user_pass']){
            return myjson('0','登录密码输入不正确!');
        }
        
        $res=db('members')->where('id',$useinfo['id'])->update(['user_pass'=>md5($pwd)]);
        if(!$res){
            return myjson('0','修改失败!');
        }
        return myjson('1','修改成功!');
        
    }
    
    //修改支付密码
    public function changePaypwd(){
        $params=getparam();
        if(!isset($params['mobile']) || !is_mobile($params['mobile'])){
            return myjson('0','手机号码输入不正确!');
        }
        $mobile=trim($params['mobile']);
        
        if(!isset($params['code']) || !is_numeric($params['code'])){
            return myjson('0','短信验证码不能为空!');
        }
        $code=trim($params['code']);
        
        if(!isset($params['pay_pwd']) || empty($params['pay_pwd'])){
            return myjson('0','支付密码不能为空!');
        }
        if(!preg_match("/^[a-zA-Z0-9_]{6,18}$/", $_POST['pay_pwd'])){
            return myjson('0','支付密码格式不正确!');
        }
        $pwd=trim($params['pay_pwd']);
        
        if(!check_sms_code($mobile, $code)){
            if (cache('sms_'.$mobile) == $code) {
                return myjson('0','短信验证码输入超时！');
            }else{
                return myjson('0','短信验证码不正确！');
            }
        }
        
        $uid=$this->uid;
        db('members')->where('id',$uid)->update(['pin_pass'=>md5($pwd)]);

        return myjson('1','修改成功!');
    }
    
    //实名认证
    public function checkIdcar(){
        $params=getparam();
        if(!isset($params['real_name']) || empty($params['real_name'])){
            return myjson('0','真实姓名不能为空!');
        }
        $real_name=trim($params['real_name']);
        
        if(!isset($params['idcar']) || empty($params['idcar'])){
            return myjson('0','身份证号不能为空!');
        }
        $idcar=trim($params['idcar']);


        $cacheidcar=cache($idcar);
        if(isset($cacheidcar) && !empty($cacheidcar)){
            return myjson('0','已申请验证，请不要重复申请！');
        }
        
        $sign=time().random(6,'123456789');
        cache($idcar,$sign);
        $member = new \app\member\model\Member();
        $result=$member->realname_do($this->uid, $real_name, $idcar, $sign);
        cache($idcar,null);
        if($result['code']=='0'){
            return myjson('0',$result['msg']);
        }
        return myjson('1','实名成功！');
    }
    
    //修改用户信息
    public function upmembinfo(){
        $params=getparam();
        $memData=array();
        
        $memData['sex']=(isset($params['sex']) && in_array(trim($params['sex']), ['0','1']))?trim($params['sex']):'';
        $memData['marry']=(isset($params['merr']) && in_array(trim($params['merr']), ['已婚','未婚']))?trim($params['merr']):'';
        $memData['education']=(isset($params['education']) && in_array(trim($params['education']), ['高中或以下','大专','本科','研究生或以上']))?trim($params['education']):'';
        $memData['income']=(isset($params['salary']) && in_array(trim($params['salary']), ['2000元以下','2000至5000元','5000至10000元','10000至20000元','20000元以上']))?trim($params['salary']):'';
       
        $school='';
        if(isset($params['school']) && !empty($params['school'])){
            if(!preg_match('/^[0-9a-zA-Z\x{4e00}-\x{9fa5}]+$/u', trim($params['school']))){
                return myjson('0','毕业院校格式不正确!');
            };
            $school=trim($params['school']);
        }
        $memData['school']=$school;
        
        $address='';
        if(isset($params['address']) && !empty($params['address'])){
            if(!preg_match('/^[0-9a-zA-Z\x{4e00}-\x{9fa5}]+$/u', trim($params['address']))){
                return myjson('0','地址不正确!');
            };
            $address=trim($params['address']);
        }
        $memData['address']=$address;
        
        if(isset($params['head_img']) && !empty($params['head_img'])){
            $memData['head_img']=trim($params['head_img']);
        }
        $memData['uptime']=time();
        
        $reg=Db::name('member_info')->where('uid',$this->uid)->update($memData);
        if(!$reg){
            return myjson('0','修改失败!');
        }
        return myjson('1','修改成功!');
    }
    
    //删除银行卡
    public function delusebanks(){
        $params=getparam();
        if(!isset($params['id']) || empty($params['id'])){
            return myjson('0','id不能为空!');
        }
        
        $uid=$this->uid;
        $checkBank=db('member_banks')->where(['uid'=>$uid,'id'=>trim($params['id'])])->count();
        if(!$checkBank || $checkBank<1){
            return myjson('0','银行id不存在!');
        }
        $reg=db('member_banks')->where('id',trim($params['id']))->delete();
        if(!$reg){
            return myjson('0','删除失败!');
        }
        return myjson('1','删除成功!');
    }
    
    //得到省的市
    public function getcityarr(){
        $param=getparam();
        if(!isset($param['id']) || empty($param['id'])){
            return myjson('0','省级ID不能为空');
        }
        $cityarr=get_areapid_address(trim($param['id']));
        if(!$cityarr){
            return myjson('0','城市数据为空');
        }
        $opHml='';
        foreach ($cityarr as $key=>$hang){
            $opHml.='<option value="'.$hang['id'].'">'.$hang['name'].'</option>';
        }
        return myjson('1',['optionHtml'=>$opHml]);
    }
    
    //编辑银行卡
    public function savebank(){
        $param=getparam();
        $banksid=(isset($param['bank_id']) && trim($param['bank_id'])>0)?trim($param['bank_id']):-1;
        $members = new \app\member\model\Member();
        return $members->savebank_do($this->uid,$param,$banksid);
        
    }
    
    //空返回
    public function _empty(){
        return myjson('0','接口不存在');
    }
    
}